
import type { FilterRuleOptions, TypeConfig, RuleGenerationResult } from './types';
import { createBaseRule } from './ruleGeneratorUtils';

const normalEquipmentConfig: TypeConfig = {
    class: null,  // 将在规则中动态设置
    icon: 'White',
    iconShape: 'Circle',
    name: '普通装备',
    fontSize: 40
};

// 装备类型配置
const equipmentClasses = {
    weapons: [
        'Bow', 'Claw', 'Dagger', 'One Hand Axe', 'One Hand Mace', 'One Hand Sword',
        'Sceptre', 'Staff', 'Two Hand Axe', 'Two Hand Mace', 'Two Hand Sword', 'Wand'
    ],
    armour: [
        'Body Armour', 'Boots', 'Gloves', 'Helmet', 'Shield'
    ],
    accessories: [
        'Amulet', 'Belt', 'Ring'
    ]
};

export function generateNormalEquipmentRule(options: FilterRuleOptions): RuleGenerationResult {
    let baseResult = createBaseRule(options, {
        name: normalEquipmentConfig.name,
        class: null,  // 将在后面设置具体的类型
        defaultColor: '#FFFFFF',
        defaultBgColor: '#000000',
        fontSize: normalEquipmentConfig.fontSize,
        icon: normalEquipmentConfig.icon,
        iconShape: normalEquipmentConfig.iconShape
    });

    // 如果规则无效或被禁用，直接返回
    if (!baseResult.isValid || !baseResult.content) {
        return baseResult;
    }

    const lines = baseResult.content.split('\n');
    let insertIndex = 1;  // 在Show行之后插入

    // 添加装备类型条件
    let equipmentType = '';
    if (options.value.includes('Weapon')) {
        equipmentType = 'weapons';
        lines.splice(insertIndex, 0, `  Class ${equipmentClasses.weapons.map(c => `"${c}"`).join(' ')}`);
    } else if (options.value.includes('Armour')) {
        equipmentType = 'armour';
        lines.splice(insertIndex, 0, `  Class ${equipmentClasses.armour.map(c => `"${c}"`).join(' ')}`);
    } else if (options.value.includes('Accessory')) {
        equipmentType = 'accessories';
        lines.splice(insertIndex, 0, `  Class ${equipmentClasses.accessories.map(c => `"${c}"`).join(' ')}`);
    }

    // 添加基础要求
    lines.push('  Rarity <= Rare');  // 普通到稀有度
    lines.push('  ItemLevel >= 60');  // 物品等级要求

    // 添加品质要求
    if (options.value.includes('Quality')) {
        lines.push('  Quality >= 15');
    }

    // 添加连接要求
    if (options.value.includes('Linked')) {
        const linkCount = options.value.match(/(\d+)L/)?.[1];
        if (linkCount) {
            lines.push(`  LinkedSockets >= ${linkCount}`);
        }
    }

    // 添加颜色要求
    if (options.value.includes('RGB')) {
        lines.push('  SocketGroup RGB');
    }

    // 根据装备类型添加特定要求
    if (equipmentType === 'armour') {
        lines.push('  BaseDefencePercentile >= 50');  // 基础防御至少50%
    } else if (equipmentType === 'weapons') {
        lines.push('  BaseDefencePercentile >= 50');  // 基础伤害至少50%
    }

    // 设置显示效果
    if (options.displayEnabled) {
        const quality = options.value.includes('Quality');
        const linked = options.value.includes('Linked');
        const rgb = options.value.includes('RGB');

        if (quality && linked && rgb) {
            lines.push('  SetBorderColor 255 255 255');  // 白色边框
            lines.push('  PlayEffect White');
        } else if (quality || linked || rgb) {
            lines.push('  SetBorderColor 255 255 0');    // 黄色边框
            lines.push('  PlayEffect Yellow');
        }
    }

    baseResult.content = lines.join('\n');
    return baseResult;
}